added SSCLI 1.0
[windows-sources.git] / shared source / sscli20 / tools / nmake / init.cpp
blob7e8c995db346ba09bb937c4826cc0cafb9716516
1 // ==++==
2 //
3 //
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
5 //
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
10 //
11 // You must not remove this notice, or any other, from this software.
12 //
14 // ==--==
15 // INIT.C -- routines to handle TOOLS.INI
17 // Purpose:
18 // Module contains routines to deal with TOOLS.INI file. Functions in TOOLS.LIB
19 // have not been used because NMAKE needs to be small and the overhead is too
20 // much.
22 #include "precomp.h"
23 #ifdef _MSC_VER
24 #pragma hdrstop
25 #endif
27 // findTag()
29 // arguments: tag pointer to tag name to be searched for
31 // actions: reads tokens from file
32 // whenever it sees a newline, checks the next token
33 // to see if 1st char is opening paren
34 // if no, reads and discards rest of line and
35 // checks next token to see if it's newline or EOF
36 // and if newline loops to check next token . . .
37 // if yes ('[' found), looks on line for tag
38 // if tag found, looks for closing paren
39 // if ']' found, discards rest of line and returns
40 // else keeps looking until end of file or error
42 // returns: if successful, returns TRUE
43 // if tag never found, returns FALSE
45 BOOL
46 findTag(
47 char *tag
50 BOOL endTag; // TRUE when find [...]
51 size_t n;
52 char *s;
54 for (line = 0; fgets(buf, MAXBUF, file); ++line) {
55 if (*buf == '[') {
56 endTag = FALSE;
57 for (s = _tcstok(buf+1," \t\n");
58 s && !endTag;
59 s = _tcstok(NULL," \t\n")
60 ) {
61 n = _tcslen(s) - 1;
63 if (s[n] == ']') {
64 endTag = TRUE;
65 s[n] = '\0';
68 if (!_tcsicmp(s,tag)) {
69 return(TRUE);
75 if (!feof(file)) {
76 currentLine = line;
77 makeError(0, CANT_READ_FILE);
80 return(FALSE);
84 // tagOpen()
86 // arguments: where pointer to name of environment variable
87 // containing path to search
88 // name pointer to name of initialization file
89 // tag pointer to name of tag to find in file
91 // actions: looks for file in current directory
92 // if not found, looks in each dir in path (semicolons
93 // separate each path from the next in the string)
94 // if file is found and opened, looks for the given tag
96 // (if ported to xenix, tagOpen() and searchPath()
97 // should probably use access() and not findFirst().)
99 // returns: if file and tag are found, returns pointer to file,
100 // opened for reading and positioned at the line
101 // following the tag line
102 // else returns NULL
104 BOOL
105 tagOpen(
106 char *where,
107 char *name,
108 char *tag
111 char szPath[_MAX_PATH];
113 // Look for 'name' in current directory
114 if (_access(name, READ) != 0) {
115 return FALSE;
117 strncpy(szPath, name, sizeof(szPath));
119 if (!(file = FILEOPEN(szPath, "rt"))) {
120 makeError(0, CANT_READ_FILE, szPath);
123 if (findTag(tag)) {
124 return(TRUE); // look for tag in file
127 if (fclose(file) == EOF) { // if tag not found, close
128 makeError(0, ERROR_CLOSING_FILE, szPath);
131 return(FALSE); // file and pretend file not found
136 // searchPath()
138 // arguments: p pointer to string of paths to be searched
139 // name name of file being searched for
141 // actions: looks for name in current directory, then each
142 // directory listed in string.
144 // returns: pointer to path spec of file found, else NULL
146 // I don't use _tcstok() here because that modifies the string that it "token-
147 // izes" and we cannot modify the environment-variable string. I'd have to
148 // make a local copy of the whole string, and then make another copy of each
149 // directory to which I concatenate the filename to in order to test for the
150 // file's existence.
152 char *
153 searchPath(
154 char *p,
155 char *name,
156 void *findBuf,
157 NMHANDLE *searchHandle
160 char *s; // since it's not in use
163 // We use FindFirst() because the dateTime of file matters to us
164 // We don't need it always but then access() probably uses findFirst()
166 if (findFirst(name, findBuf, searchHandle)) { // check current dir first
167 return(makeString(name));
170 // Check if environment string is NULL. Unnecessary if check is done
171 // elsewhere, but it's more convenient and safer to do it here.
173 if (p == NULL) {
174 return(NULL);
177 for (s = buf; ;) {
178 while (*p && '\"' == *p) {
179 // Quotes should not be used in search paths. If we find any,
180 // we ignore them. This way we can form the full path and the
181 // filename without quotes and add an enclosing pair of quotes
182 // later, if necessary.
183 p++;
185 if (!*p || (*s = *p++) == ';') { // found a dir separator
186 if (s == buf) { // ignore ; w/out name
187 if (*p) {
188 continue;
191 return(NULL); // list exhausted ...
194 if (!IsPathSeparator(*(s-1))) { // append path separator
195 *s++ = PATH_SEPARATOR_CHAR;
198 *s = '\0';
200 if (_tcspbrk(buf,"*?")) { // wildcards not allowed
201 s = buf;
202 continue;
205 _tcscpy(s, name); // append file name, zap
207 if (findFirst(buf, findBuf, searchHandle)) {
208 return(makeString(buf));
211 s = buf; // reset ptr to begin of
212 } // buf and check next dir
213 else {
214 ++s; // we keep copying chars
215 } // until find ';' or '\0'